﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using DrawToolsLib;
using Modelica.ModelicaTypes;
using OmcSocketClient;

using Modelica.CoreLite;

namespace DrawTools {

    /// <summary>
    /// This class represents single panel for editing single Icon.
    /// </summary>
    public partial class IconEditor : DockPanel {

        #region Members
        public ModelicaClass Mclass { get; internal set; }
        #endregion Members

        public IconEditor(ModelicaClass mclass) {
            if (mclass == null) {
                throw new Exception("IconEditor null mclass in constructor.");
            }
            this.Mclass = mclass;

            InitializeComponent();

            InitializeTextEditor();

            InitializeDrawingCanvas();
            drawingCanvas.ActiveIcon = Mclass.Icon;
        }

        /// <summary>
        /// Set initial properties of drawing canvas
        /// </summary>
        private void InitializeTextEditor() {
            textEditor.SetHighlighting("Modelica");

            // workaround, aby se netahaly ze serveru definice ukazkovych ikon
            if (!Mclass.Name.StartsWith("Icon ")) {
                // je to len demonstracia, taky hnusny kus kodu tu potom nesmie byt
                string code = omc.List(Mclass.QualifiedName);
                this.textEditor.Text = code.Trim(new char[] { '\r', '\n' }).Trim(new char[] { '"' });
            } else {
                this.textEditor.Text = "Hello world!";
            }
        }

        /// <summary>
        /// Set initial properties of drawing canvas
        /// </summary>
        private void InitializeDrawingCanvas() {
            drawingCanvas.LineWidth = 1;
            drawingCanvas.ObjectColor = Colors.Black;

            drawingCanvas.TextFontSize = 12;
            drawingCanvas.TextFontFamilyName = "Tahoma";
            drawingCanvas.TextFontStyle = FontStyles.Normal;
            drawingCanvas.TextFontWeight = FontWeights.Normal;
            drawingCanvas.TextFontStretch = FontStretches.Normal;
        }

        #region Button handlers and Commands

        /// <summary>
        /// Subscribe to different events
        /// </summary>
        void SubscribeToEvents() {
        }
        
        /// <summary>
        /// Undo command
        /// </summary>
        public void UndoCommand_Executed(object sender, ExecutedRoutedEventArgs args) {
            drawingCanvas.Undo();
        }

        /// <summary>
        /// Undo command
        /// </summary>
        private void UndoCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = drawingCanvas.CanUndo;
        }

        /// <summary>
        /// Redo command
        /// </summary>
        public void RedoCommand_Executed(object sender, ExecutedRoutedEventArgs args) {
            drawingCanvas.Redo();
        }

        /// <summary>
        /// Undo command
        /// </summary>
        private void RedoCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = drawingCanvas.CanRedo;
        }

        /// <summary> 
        /// One of Tools toolbar buttons is clicked.
        /// ToolType enumeration member name is in the button tag.
        /// </summary>
        public void SelectTool_Executed(object sender, ExecutedRoutedEventArgs args) {
            DrawingTools.ToolType = (ToolType)Enum.Parse(typeof(ToolType),
               ((string)args.Parameter));

            args.Handled = true;
        }

        /// <summary>
        /// Len docasna funkcia, aby ked sa zmeni ikona v editore, tak pri prepnuti tabu
        /// na kod sa poslala na omc a natiahol sa novy kod.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TopTab_SelectionChanged( object sender, SelectionChangedEventArgs e ) {
            //helpTxt.Text += "saving.... \n";

            // TODO: Smazat podminku az se v aplikaci nebudou vyskytovat testovanci ikony vpravo v list boxu
            if (!Mclass.QualifiedName.StartsWith("Icon ")) {
                Core.UpdateClassIcon(this.Mclass);
                // je to len demonstracia, taky hnusny kus kodu tu potom nesmie byt
                string code = omc.List(Mclass.QualifiedName);
                this.textEditor.Text = code.Trim(new char[] { '\r', '\n' }).Trim(new char[] { '"' });
            }
        }

        #endregion Button handlers and Commands


        #region Other functions


        #endregion Other functions
    }
}
